package com.sf.osms.dep.quartz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sf.osms.dep.biz.ICommonServiceBiz;
import com.sf.osms.dep.cache.SysConfigCacheHelper;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.processor.util.DateUtil;
import com.sf.osms.dep.processor.util.RouterConstants;
import com.sf.osms.dep.processor.util.WebserviceUtil;
import com.sf.osms.dep.webservice.fvp.BarRecordBaseDto;

/**
 * @author sfhq1469
 * 定时任务工作的明细类
 */
@Component
public class DepRouteUploadJobDetail {
	private static Logger logger = LoggerFactory.getLogger(DepRouteUploadJobDetail.class);
	
	@Autowired
	private ICommonServiceBiz commonService;
	public void depRouteUpload(){
		
		//logger.info("路由定时推送任务开始执行");
		
		boolean	result = false;
		//查找路由表中待发送的路由信息
		List<Router> routerlist = commonService.queryRouterByStatus(RouterConstants.STATUS);
		//申明发送FVP参数集合
		List<BarRecordBaseDto> barRecordBaseDtos = new ArrayList<BarRecordBaseDto>();
		if(null != routerlist && routerlist.size() > 0){
			try {
				logger.info("转化BarRecordBaseDto对象开始");
				for (Router router : routerlist) {
					//设置FVP巴枪时间
					Date oldBarDate = router.getBarDate();
					String date = DateUtil.dateFomat(oldBarDate, "yyyy-MM-dd");
					Date oldBarTime = router.getBarTime();
					String time = DateUtil.dateFomat(oldBarTime, "HH:mm:ss");
					router.setFvpBarScanTm(DateUtils.parseDate(date + time, new String[] {
							"yyyy-MM-ddHH:mm:ss"
					}));
					//对象映射
					BarRecordBaseDto brb = commonService.mapBean(router, BarRecordBaseDto.class);
					if(StringUtils.isNotBlank(brb.getPhoneZone()) && brb.getPhoneZone().length() > 20){
						String newPhoneZone = brb.getPhoneZone().substring(0, 20);
						brb.setPhoneZone(newPhoneZone);
					}
					//映射字段
					String depSysternName = SysConfigCacheHelper.getSysConfigValue("systerm_name");
					if(StringUtils.isBlank(depSysternName)){
						depSysternName = RouterConstants.AGEN_BAR_SN;
					}
					brb.setBarSn(depSysternName.trim());
					barRecordBaseDtos.add(brb);
					logger.info("转化为,BarRecordBaseDto记录："+brb.toString());	
				}
				
				//fvp返回结果
				result = WebserviceUtil.invokeFvp(barRecordBaseDtos);
				
				logger.info("路由推送结果：" + result);
				
			} catch (Exception e) {
				logger.error("路由推送发生异常!");
				logger.error(e.getMessage(), e);
			}
			
			//当发送成功,修改路由状态为0,如果失败次数大于5次，则修改状态为1且不再发送
			if(result == true){
				StringBuffer mailnos = new StringBuffer();
				for (Router router : routerlist) {
					router.setSendCount(router.getSendCount() == null ? 1 : (router.getSendCount() + 1));
					router.setStatus(0);
					mailnos.append(router.getSfWaybill()).append(",");
					router.setStatusDt(new Date());
				}
				
				logger.info("路由推送成功，成功的运单号为：" + mailnos);
				
			}else{
				StringBuffer mailnos = new StringBuffer();
				for (Router router : routerlist) {
					mailnos.append(router.getSfWaybill()).append(",");
					router.setSendCount(router.getSendCount() == null ? 1 : (router.getSendCount() + 1));
					if(router.getSendCount() >= 5){
						router.setStatus(1);
					}
					router.setStatusDt(new Date());
				}
				logger.info("路由推送失败，失败的运单号为：" + mailnos);
				
			}
			commonService.updateRoute(routerlist);
		}
		else{
			//logger.info("没有需要推送的路由信息,方法执行完毕");
		}
		
	}

}
